package com.dronff.gvm;

import com.dronff.gvm.entity.Klass;
import com.dronff.gvm.interfaces.ReadFunction;
import com.dronff.gvm.interfaces.WriteFunction;
import com.dronff.gvm.memorymanager.Memory;
import com.dronff.gvm.memorymanager.SafeBytes;
import com.dronff.gvm.memorymanager.impl.AllocatableMemoryImpl;
import com.dronff.gvm.memorymanager.impl.MiniMemoryImpl;
import com.dronff.gvm.rtda.heap.Heap;
import com.dronff.gvm.utils.ByteUtils;
import com.dronff.gvm.utils.MemoryAllocUtils;

import java.util.Arrays;

/**
 * @author: tyf
 * @date: 2022/9/9 9:42
 * @description: todo(你干嘛 哈哈 哎哟)
 */
public class Main {
    public static void main(String[] args) throws IllegalAccessException {
        // 5m
        GvmEnv.getInstance().setMaximumHeap(1024 * 30);
        GvmEnv.getInstance().setYoungGenSize(1024 * 10);
        AllocatableMemoryImpl memory = AllocatableMemoryImpl.newInstance(1024);
        AllocatableMemoryImpl m2 = memory.allocRegion(50);
        AllocatableMemoryImpl m3 = memory.allocRegion(50);
        AllocatableMemoryImpl m4 = m3.allocRegion(20);
        AllocatableMemoryImpl m5 = m3.allocRegion(20);
        System.out.println(m2.getOffset());
        System.out.println(m3.getOffset());
        System.out.println(m5.getOffset());
        MiniMemoryImpl alloc = m4.alloc(20);
        System.out.println(alloc.getAddress());
        MiniMemoryImpl memory1 = alloc.getAddress().getMemory();
        memory1.writeOnly((WriteFunction<SafeBytes>) data -> data.set(1, (byte) 1));
        System.out.println(Arrays.toString(memory1.readOnly((ReadFunction<byte[]>) data -> data)));
        System.out.println(memory1.readOnly(data -> data).length);
        System.out.println(memory1.getSize());
        System.out.println(Arrays.toString(alloc.readOnly((ReadFunction<byte[]>) data -> data)));
        System.out.println("m4"+alloc.readOnly(data -> data).length);
    }
}
